package com.hnust.controller;

import com.hnust.domain.College;
import com.hnust.domain.dto.TeaNum;
import com.hnust.fx.DialogBuilder;
import com.hnust.fx.FXMLController;
import com.hnust.fx.GUIState;
import com.hnust.service.TeacherServices;
import com.jfoenix.controls.JFXDialog;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;


@FXMLController
public class LoginController implements Initializable {
    private static final Logger logger = LogManager.getLogger(LoginController.class);
    public TextField loginAccount;
    public PasswordField loginPassword;
    public TextField loginCode;
    public StackPane loginWindow;
    public TextField registerName;
    public TextField registerTeaNumber;
    public TextField registerTeaEmail;
    public PasswordField registerPassword;
    public PasswordField registerRepeatPassword;
    public TextField registerPhone;
    public GridPane loginContainer;
    public GridPane registerContainer;
    public Label teaTitle;
    public ComboBox<College> registerCollege;
    public Button loginBut;
    public Button registerBtn;
    public JFXDialog dialog;
    public Label registerCollegeLabel;
    private String selectCollegeId;
    private Stage stage;
    @Autowired
    private TeacherServices teacherServices;

    public void initialize(URL location, ResourceBundle resources) {
        stage= GUIState.getStage();
        stage.setMinWidth(950);
        stage.setMinHeight(700);
        registerCollege.setConverter(new StringConverter<College>() {
            @Override
            public String toString(College object) {
                return object.getName();
            }
            @Override
            public College fromString(String string) {
                return null;
            }
        });
        registerCollege.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                selectCollegeId=newValue.getColId();
               logger.info("选中的学院ID："+selectCollegeId);
            }
        });
    }

    public void loginButtonClick(ActionEvent event) {
        TeaNum.teaNum=loginAccount.getText();

        logger.info("用户点击登录");
        teacherServices.teacherLogin(loginBut,stage,loginAccount.getText(),loginPassword.getText());

    }

    public void registerButtonClick(ActionEvent actionEvent) {
        logger.info("用户点击注册");
        if(registerPassword.getText().equals("")||registerPassword.getText()==null||
        registerRepeatPassword.getText().equals("")||registerRepeatPassword.getText()==null){
            new DialogBuilder(registerBtn).setTitle("提示").setMessage("密码为必填项").
                    setPositiveBtn("确定","#ff3333").create();
        }else{
            if(registerPassword.getText().equals(registerRepeatPassword.getText())){
                if(registerName.getText().equals("")||registerName.getText()==null){
                    new DialogBuilder(registerBtn).setTitle("提示").setMessage("名字为必填项").
                            setPositiveBtn("确定","#ff3333").create();
                }else if(registerPhone.getText().equals("")||registerPhone.getText()==null){
                    new DialogBuilder(registerBtn).setTitle("提示").setMessage("手机号为必填项").
                            setPositiveBtn("确定","#ff3333").create();
                }else if(selectCollegeId==null){
                    new DialogBuilder(registerBtn).setTitle("提示").setMessage("请先选择学院").
                            setPositiveBtn("确定","#ff3333").create();
                }
                else if(registerTeaNumber.getText().equals("")||registerTeaNumber.getText()==null){
                    new DialogBuilder(registerBtn).setTitle("提示").setMessage("教工号为必填项").
                            setPositiveBtn("确定","#ff3333").create();
                }else {
                    logger.info("验证成功，开始执行注册");
                    teacherServices.teacherRegister(registerBtn,registerName.getText(),registerTeaNumber.getText(),registerPassword.getText(),
                            registerPhone.getText(),registerTeaEmail.getText(),selectCollegeId);
                }
            }else{
                new DialogBuilder(registerBtn).setTitle("提示").setMessage("两次输入的密码不一致").
                        setPositiveBtn("确定","#ff3333").create();
            }
        }

    }

    public void shiftRegister(MouseEvent mouseEvent) {
        teaTitle.setText("教 师 注 册");
        loginContainer.setManaged(false);
        loginContainer.setVisible(false);
        registerContainer.setManaged(true);
        registerContainer.setVisible(true);
        teacherServices.getAllCollege(registerCollege,registerCollegeLabel);
    }

    public void shiftLogin(MouseEvent mouseEvent) {
        teaTitle.setText("教 师 登 录");
        loginContainer.setManaged(true);
        loginContainer.setVisible(true);
        registerContainer.setManaged(false);
        registerContainer.setVisible(false);
    }
}
